package com.yanqu.road.dao.impl.palace;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.estatewar.palace.UserPalaceData;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserPalaceDataDaoImpl extends TempDataObjectDao<UserPalaceData> {

    public Map<Long, UserPalaceData> getUserPalaceDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserPalaceData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_palace_data where activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserPalaceData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserPalaceData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_palace_data(activityId,userId,pos,worshipTime,onlyUnionMemberJoin,underAide) " +
                "values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPos());
            call.setLong(i++, data.getWorshipTime());
            call.setBoolean(i++, data.isOnlyUnionMemberJoin());
            call.setInt(i++, data.getUnderAide());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserPalaceData data) {
        PreparedStatement call = null;
        String sql = "update t_u_palace_data set pos=?,worshipTime=?,onlyUnionMemberJoin=?,underAide=?" +
                " where activityId=? and userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getPos());
            call.setLong(i++, data.getWorshipTime());
            call.setBoolean(i++, data.isOnlyUnionMemberJoin());
            call.setInt(i++, data.getUnderAide());
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserPalaceData parseData(ResultSet rs) throws SQLException {
        UserPalaceData data = new UserPalaceData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPos(rs.getInt("pos"));
        data.setWorshipTime(rs.getLong("worshipTime"));
        data.setOnlyUnionMemberJoin(rs.getBoolean("onlyUnionMemberJoin"));
        data.setUnderAide(rs.getInt("underAide"));
        return data;
    }
}
